// dojo.io.script is an external dependency, so it must be required
dojo.require("dojo.io.script");
// When the resource is ready
query_dict = {
            callback:'getJson',
			q:'',
			sl:'en',
			tl:'zh-CN',
			restrict:'pr,we,ex',
			client:'te',
        }

query_suggestion = {
	ds:'d',
	hl:'zh-CN',
	jsonp:'getSuggest',
	q:'',
	
}

query_translate = {
	client:'t',
	text:'',
	hl:'en',
	sl:'en',
	tl:'zh-CN'

}

var url_controller;


function searchFromUrl(){
	var url = window.location.href;
	var index = url.search("#");
	if(index==-1){
		return
	}
	url = url.substring(index+1).split("/");
	query_dict.q = url[2];
	query_suggestion.q = url[2];
	query_dict.sl = url[0];
	
	query_dict.tl = url[1];
	query_suggestion.hl = url[1];
	
	
	document.getElementById("selectfrom").value =url[0];
	document.getElementById("selectto").value =url[1];
	
	document.getElementById("form_value").value = url[2] ;
	
	instant();
}


function controlUrl(word){
	var from = document.getElementById("selectfrom").value;
	var to = document.getElementById("selectto").value;
	query_dict.sl = from;
	
	query_dict.tl = to;
	query_suggestion.hl = to;

	
	
	if( typeof(word)== "undefined"){
		word = query_dict.q;
		
	}
	
	window.location = "#" +from +"/" +to+"/" +  word;
}

function instant_search(word) {
	
	query_dict.q = word;
 
    // Use the get method
    dojo.io.script.get({
        // The URL to get JSON from Twitter
        url: "http://www.google.com/dictionary/json",
        // The callback paramater
        callbackParamName: "callback", 
        // The content to send
        content: query_dict,
        // The success callback
        load: function(dict) {  // Twitter sent us information!

            var content = process_primary(dict.primaries);
			var webdef = process_primary(dict.webTranslations);
			document.getElementById("content").innerHTML = content;
			changeSound();
			document.getElementById("webdef").innerHTML = webdef;
			
        }
    });
}

function instant(){
	
	var value = document.getElementById("form_value");
	query_suggestion.q =  dojo.trim(value.value);
	
	clearTimeout(url_controller);
	url_controller = setTimeout("controlUrl(query_suggestion.q)",600);
	
	// Use the get method
    dojo.io.script.get({
        url: "http://suggestqueries.google.com/complete/search",
        // The callback paramater
        callbackParamName: "jsonp",
        // The content to send
        content: query_suggestion,
        // The success callback
        load: function(suggestion) {  // Twitter sent us information!
			
			var sg = suggestion[1];
	        var content = "";
			for(var i=0;i<sg.length;i++){
				sgitem = sg[i];
				content +='<div class = "sgitem"> <label>'
				 + sgitem[0] + '</label><text class="sgsence">' + sgitem[1].substring(0,20)+ '</text></div>';
			}
			
			document.getElementById("suggest").innerHTML = content;
			
			var sgitems = dojo.query(".sgitem");
			for (var i =0;i <sgitems.length;i++){
				sgitem = sgitems[i];
				dojo.connect(sgitem,'mouseover',function () {
					this.style.backgroundColor = "#EBEFF9";
					
				});
				
				dojo.connect(sgitem,'mouseout',function () {
					this.style.backgroundColor = "white";
				});
				
				dojo.connect(sgitem,'onclick',function(){
					var word = this.childNodes[1].innerHTML;
					if(word ==''){word = this.childNodes[0].innerHTML;}
					document.getElementById("form_value").value = word;
					instant();
					controlUrl();
				});
				
			};
			if(sg.length <=0){
				instant_search(query_suggestion.q);
			}
			else{
				instant_search(sg[0][0]);
			}
			
        }
    });
}




function process_primary(dict_primary){
    var primary = dict_primary;
    var xml = "";
    if(primary instanceof Array){
        for(var i in primary){
            var data = primary[i];
            xml += process_primary(data);
        }
    }
    
    else if(typeof(primary) == "object"){
        var hasChild = false;
        var hasType = false;
        
        if(primary.type != undefined){
            hasType = true;
            if(primary.type == "container"){
            	xml += '<div class="wordtype">' + primary.labels[0].text + ':</div>';
            }
            
            xml += '<div class="'+ primary.type + '">'+process_terms(primary.terms,primary.type);


            if(primary.entries != undefined){
 
                xml += process_primary(primary.entries);
            }
            xml += "</div>";
        }
    }
    return xml;
}              

function process_terms(dict_terms,type){
	var terms = dict_terms
	var xml = ""
	if(terms instanceof Array){
        for(var i in terms){
            var data = terms[i];
            xml += process_terms(data,type);
        }
    }
    
    else if(typeof(terms) == "object"){
        var hasType = false;
        if(terms.type != undefined){
            hasType = true;
            if(terms.type !="text" || type == "headword" || type =="related"){
            	xml += '<div class="'+ terms.type + '">';
           		xml += terms.text +"</div>";
            	
            }
            else{
            	xml += terms.text;
            }

        }
    }
    return xml
}


function process_term(dict_terms){
	var terms = dict_terms

    for(var i in terms){
    	var data = terms[i];
        xml += (data.text);
    }

    return xml
}

function changeSound(){
	var sounds = dojo.query(".sound");
	for(var i=0;i<sounds.length;i++){
		sounds[i].innerHTML = getSound(escape(sounds[i].innerHTML));
	}

}
function getSound(url){
	
	var thesound = '<object data="http://www.google.com/dictionary/flash/SpeakerApp16.swf" type="application/x-shockwave-flash" width=" 16" height="16" id="pronunciation">' +
			'<param name="movie" value="http://www.google.com/dictionary/flash/SpeakerApp16.swf">' +
			'<param name="flashvars" value="sound_name='+ url+'">' +
			'</object>';
	return thesound;
}














